//
//  main.m
//  KVC
//
//  Created by 移云 on 2019/10/14.
//  Copyright © 2019 猫爷爷的酱醋茶. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"
#import "Person.h"

void setValueForKey(void);

//字典转模型
void setValueForKeyWithDic(void);

void setValueForKeyPath(void);

//模型转字典
void dicWithValueForKey(void);

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        dicWithValueForKey();
    }
    return 0;
}

//模型转字典
void dicWithValueForKey(void)
{
    Person *p1 = [[Person alloc]init];
    Dog *dog = [[Dog alloc]init];
    [p1 setValue:@"猫爷爷的酱醋茶" forKeyPath:@"name"];
    [p1 setValue:@22 forKeyPath:@"age"];
    [p1 setValue:@"email@163.com" forKeyPath:@"email"];
    [p1 setValue:dog forKeyPath:@"dog"];
    //keyPath - 属性的路径
    [p1 setValue:@"popopi" forKeyPath:@"dog.name"];
    
    NSDictionary *dic = [p1 dictionaryWithValuesForKeys:@[@"name",@"age",@"email",@"dog"]];
    NSLog(@"%@",dic);
    
    //获取到的是Dog对象
    NSLog(@"%@",dic[@"dog"]);
    //验证
    NSLog(@"%@",[dic[@"dog"] class]);
    
    //要获取对象的值
    NSLog(@"%@",[dic[@"dog"] name]);
}


void setValueForKeyPath(void)
{
    Person *p1 = [[Person alloc]init];
    Dog *dog = [[Dog alloc]init];
    [p1 setValue:@"猫爷爷的酱醋茶" forKeyPath:@"name"];
    [p1 setValue:@22 forKeyPath:@"age"];
    [p1 setValue:@"email@163.com" forKeyPath:@"email"];
    [p1 setValue:dog forKeyPath:@"dog"];
    //keyPath - 属性的路径
    [p1 setValue:@"popopi" forKeyPath:@"dog.name"];
    
    NSLog(@"%@_______%d_______%@_______%@",p1.name,p1.age,p1.email,p1.dog.name);
    
    
    NSString *personName = [p1 valueForKeyPath:@"name"];
    NSString *dogName = [p1 valueForKeyPath:@"dog.name"];
     NSLog(@"%@_______%@",personName,dogName);
    
}





void setValueForKeyWithDic(void)
{
    Person *iOSDev = [[Person alloc]init];
    
    NSDictionary *dic = @{
                          @"name":@"猫爷爷的酱醋茶",
                          @"age":@22,
                          @"email":@"email@163.com",
                          @"dog":@{@"name":@"popop1"}
                          };
    
    [iOSDev setValuesForKeysWithDictionary:dic];
    NSDictionary *dogDic = (NSDictionary *)iOSDev.dog;
    NSLog(@"%@_______%d_______%@_______%@",iOSDev.name,iOSDev.age,iOSDev.email,dogDic[@"name"]);
}

void setValueForKey(void)
{
    Person *p1 = [[Person alloc]init];
    [p1 setValue:@"猫爷爷的酱醋茶" forKey:@"name"];
    [p1 setValue:@22 forKey:@"age"];
    [p1 setValue:@"email@163.com" forKey:@"email"];
    
    NSLog(@"%@_______%d_______%@",p1.name,p1.age,p1.email);
    
}




